package com.baorant.seckillbackservice.service;

import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class SaveOrderService {
	@Autowired
	private StringRedisTemplate template;
	final Logger LOG = LoggerFactory.getLogger(SaveOrderService.class);

	@Scheduled(fixedRate = 1000)
	public void run() {
		String strOrder = template.opsForList().leftPop("seckill.order");
		if (strOrder == null || strOrder == "")
			return;
		System.out.println("SaveOrderService leftPop strOrder: " + strOrder);
		String arr[] = strOrder.split(",");
		if (arr.length < 3)
			return;
		String ticket = arr[0];
		String strGoodsid = arr[1];
		String strTime = arr[2];
		int goodsid = 0;
		goodsid = Integer.parseInt(strGoodsid);
		long time = 0;
		time = Long.parseLong(strTime);
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		strTime = format.format(time);
		//保存订单记录
		LOG.info("订单记录：goodsid:"+goodsid+", ticket:"+ticket+", time:"+strTime);
		// 数据库中库存减1
		LOG.info("数据库中库存减1：goodsid:"+goodsid);		
	}
}
